package com.stockz.core.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.hibernate.annotations.Index;
import org.springframework.context.annotation.Lazy;

@Entity
@Lazy(true)
@IdClass(BarId.class)
@NamedQueries({@NamedQuery(name=Bar.getBarListByCodeQuery,query="select b from Bar b where b.code = :code order by b.date desc")})
public class Bar implements Serializable,Comparable<Bar> {
	private static final long serialVersionUID = -3390964550350015737L;
	public static final String getBarListByCodeQuery = "getBarListByCodeQuery";
	
	private Date date;
	private String code;
	private double open;
	private double high;
	private double low;
	private double close;
	private double last;
	private double previousClose;
	private double volume;
	private double turnover;
	private double trades;
	
	public boolean equals(Object o) {
		if(o != null && o instanceof Bar){
			Bar bar = (Bar)o;
			if(code != null && date != null && bar.code != null && bar.date != null){
				return code.trim().equalsIgnoreCase(bar.code.trim()) && date.equals(bar.date);
			}
		}
		return super.equals(o);
	}
	
	@Override
	public int hashCode() {
		if(code != null && date != null){
			return Integer.parseInt(String.valueOf(code.hashCode())+String.valueOf(date.hashCode()));
		}
		return super.hashCode();
	}
	
	public String toString() {
		return date.toString()+code;
	}
	
	public int compareTo(Bar bar) {
		if(date != null && bar.date != null){
			return date.compareTo(bar.date)*-1;
		}
		return 0;
	}
	
	@Id
	@Column(nullable=false)
	@Index(name="barCodeIndex")
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	@Id
	@Column(nullable=false)
	@Index(name="barDateIndex")
	public Date getDate() {
		return date;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}
	
	@Column
	public double getOpen() {
		return open;
	}
	public void setOpen(double open) {
		this.open = open;
	}
	@Column
	public double getHigh() {
		return high;
	}
	public void setHigh(double high) {
		this.high = high;
	}
	@Column
	public double getLow() {
		return low;
	}
	public void setLow(double low) {
		this.low = low;
	}
	@Column
	public double getClose() {
		return close;
	}
	public void setClose(double close) {
		this.close = close;
	}
	@Column
	public double getVolume() {
		return volume;
	}
	public void setVolume(double volume) {
		this.volume = volume;
	}
	@Column
	public double getTurnover() {
		return turnover;
	}
	public void setTurnover(double turnover) {
		this.turnover = turnover;
	}
	@Column
	public double getTrades() {
		return trades;
	}
	public void setTrades(double trades) {
		this.trades = trades;
	}
	@Column
	public double getLast() {
		return last;
	}
	public void setLast(double last) {
		this.last = last;
	}
	@Column
	public double getPreviousClose() {
		return previousClose;
	}
	public void setPreviousClose(double previousClose) {
		this.previousClose = previousClose;
	}
}
